package com.chinasofti.manager.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.chinasofti.common.IdWorker;
import com.chinasofti.dao.EmpAttenceRepository;
import com.chinasofti.dao.EmpRepository;
import com.chinasofti.manager.service.EmpAttenceService;
import com.chinasofti.model.Emp;
import com.chinasofti.model.EmpAttence;
import com.tramy.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Creator: <br>
 * Date: <br>
 * Description: <br>
 */
@Service
@Component
public class EmpAttenceImpl implements EmpAttenceService{
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private EmpRepository empRepository;
    @Autowired
    private EmpAttenceRepository empAttenceRepository;

    @Override
    public Integer clock(String name) {
        String date = DateUtils.dateToString(DateUtils.now(), "yyyy-MM-dd");
        EmpAttence preEmpAttence = empAttenceRepository.findByDate(date);
        if (preEmpAttence == null){
            //上班
            Emp emp = empRepository.findByName(name);
            EmpAttence empAttence = new EmpAttence();
            empAttence.setId(idWorker.nextId());
            empAttence.setName(emp.getName());
            empAttence.setDeptName(emp.getDeptName());

            empAttence.setDate(date);
            empAttence.setAtime(DateUtils.now());
            empAttence.setLtime(DateUtils.now());
            empAttenceRepository.save(empAttence);

            return 1;
        }else {
            if (preEmpAttence.getLtime().equals(preEmpAttence.getAtime())){
                //下班
                preEmpAttence.setLtime(DateUtils.now());
                empAttenceRepository.save(preEmpAttence);
                return 2;
            }else {
                //已完成
                return 0;
            }
        }
    }

}
